<template>
  <div class="container">
    <text class="big-title">示例</text>
    <div class="content">
      <div class="row">
        <text class="title">key: </text>
        <input placeholder="input key" class="input" :value="inputKey" @input="keyChange"></input>
        <text class="title">value: </text>
        <input placeholder="input key" class="input" :value="inputValue" @input="valueChange"></input>
        <div class="btn" @click="saveItem">
          <text class="btn-text">Save</text>
        </div>
      </div>
      <div class="divider"></div>
      <list class="list">
        <cell class="cell" v-for="item in storageData">
          <text>key: {{item.key}}, value: {{item.value}}</text>
        </cell>
      </list>
      <div class="btn" @click="clearAll">
        <text class="btn-text">Delete All</text>
      </div>
    </div>
    <text class="big-title">文档</text>
    <div class="button" @click="showDoc"><text class="btn-text">点击查看文档</text></div>
  </div>
</template>

<style scoped>
.container {
  padding: 10px;
}
.big-title {
  color: black;
  font-size: 50px;
  font-weight: bold;
  margin-bottom: 20px;
  margin-top: 20px;
}
.row {
  flex-direction: row;
  align-items: center;
}
.input {
  border-width: 2px;
  border-color: black;
  flex: 1;
  height: 60px;
  padding-left: 10px;
  padding-right: 10px;
  margin-right: 10px;
}
.btn {
  border-width: 2px;
  border-color: blue;
  border-radius: 8px;
  padding-top: 15px;
  padding-bottom: 15px;
  padding-left: 20px;
  padding-right: 20px;
  background-color: transparent;
  justify-content: center;
  align-items: center;
}
.btn:active {
  background-color: #ECECEC;
}
.divider {
  height: 1.8px;
  background-color: #ECECEC;
  margin-top: 20px;
  margin-bottom: 20px;
}
.title {
  font-size: 28px;
}
.button {
  height: 60px;
  justify-content: center;
  opacity: 1.0;
}
.button:active {
  opacity: 0.6;
}
.btn-text {
  font-size: 30px;
  color: blue;
}
</style>

<script>
  import utils from '../utils.js';
  let storage = weex.requireModule('storage');
  let modal = weex.requireModule('modal');
  let navigator = weex.requireModule('navigator');
  export default {
    data: {
      storageData: [],
      inputKey: '',
      inputValue: ''
    },
    created() {
      this.showAllData();
    },
    methods: {
      keyChange(event) {
        let v = event.value.trim();
        this.inputKey = v;
      },
      valueChange(event) {
        let v = event.value.trim();
        this.inputValue = v;
      },
      saveItem() {
        if (this.inputKey == '' || this.inputValue == '') {
          modal.toast({message: 'key or value should not be null', duration: 1});
          return;
        }
        storage.setItem(this.inputKey, this.inputValue, (event) => {
          modal.toast({message: 'set item success', duration: 1});
          this.inputKey = '';
          this.inputValue = '';
          this.showAllData();
        })
      },
      clearAll() {
        modal.confirm({message: '真的要删除所有数据吗？'}, (event) => {
          if (event == 'OK') {
            storage.getAllKeys(event => {
              let keys = event.data;
              let count = keys.length;
              if (keys && keys.length > 0) {
                for (let i = 0; i < count; i++) {
                  let key = keys[i];
                  storage.removeItem(key, event => {
                    --count;
                    if (count == 0) {
                      this.showAllData();
                    }
                  });
                }
              }
            });
          }
        });
      },
      showAllData() {
        storage.getAllKeys(event => {
          let keys = event.data;
          this.storageData = [];
          if (keys && keys.length > 0) {
            let count = keys.length;
            for (let i = 0; i < count; i++) {
              let key = keys[i];
              storage.getItem(key, event => {
                this.storageData.push({
                  key: key,
                  value: event.data,
                  index: i
                });
              });
            }
          }
        })
      },
      showDoc() {
        navigator.push({
          url: utils.getDocDetailUrl(weex.config.bundleUrl),
          animated: true
        });
      }
    }
  }
</script>
